﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MuseLibrary;

namespace MuseLibraryTests
{
    [TestClass]
    public class ExtensionsFixture
    {
        [TestMethod]
        public void CanMakeTenBitIntegers()
        {
            // 8-bit ints:
            //        1        4       16       64        1
            // 00000001 00000100 00010000 01000000 00000001
            // -----------------------------------
            // 10-bit ints:
            // 0000000100 0001000001  0000010000 0000000001
            //          4         65         16          1

            var values = new List<int> { 1,2,3, 4, 8, 32, 192, 0 };
            var newValues = values.To10BitIntegers(3, 4);
            Assert.AreEqual(newValues[0], 4);
            Assert.AreEqual(newValues[1], 2);
            Assert.AreEqual(newValues[2], 2);
            Assert.AreEqual(newValues[3], 3);
        }

        [TestMethod]
        public void CanMakeTruncatedBinary()
        {
            var median = 6;
            var results = new List<int>();
            var resultBits = new List<int>();

            for (int i = 0; i < median; i++)
            {
                int bits;
                var truncatedBinaryEncoded = i.ToTruncatedBinary(median, out bits);
                results.Add(truncatedBinaryEncoded);
                resultBits.Add(bits);
            }

            var expectedValues = new[] { 0, 1, 4, 5, 6, 7 };
            var expectedBits = new[] { 2, 2, 3, 3, 3, 3 };
            for (int i = 0; i < median; i++)
            {
                Assert.AreEqual(expectedValues[i], results[i]);
                Assert.AreEqual(expectedBits[i], resultBits[i]);
            }


            median = 23;
            results = new List<int>();
            resultBits = new List<int>();

            for (int i = 0; i < median; i++)
            {
                int bits;
                var truncatedBinaryEncoded = i.ToTruncatedBinary(median, out bits);
                results.Add(truncatedBinaryEncoded);
                resultBits.Add(bits);
            }

            expectedValues = new[] { 0,1,2,3,4,5,6,7,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31 };
            expectedBits = new[] { 4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5 };
            for (int i = 0; i < median; i++)
            {
                Assert.AreEqual(expectedValues[i], results[i]);
                Assert.AreEqual(expectedBits[i], resultBits[i]);
            }
        }

        [TestMethod]
        public void CanConvertToElias()
        {
            var bits = "000010100".ToBools();
            Assert.AreEqual(bits.ToElias(4), 20);
        }
    }
}
